package product;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Show {
    private int id; // 场次ID
    private Movie movie; // 关联影片
    private String hall; // 放映厅
    private Date time; // 放映时间
    private double price; // 票价
    private List<Seat> seats; // 座位列表

    public Show(int id, Movie movie, String hall, Date time, double price, int rowCount, int colCount) {
        this.id = id;
        this.movie = movie;
        this.hall = hall;
        this.time = time;
        this.price = price;
        // 初始化座位
        seats = new ArrayList<>();
        for (int i = 1; i <= rowCount; i++) {
            for (int j = 1; j <= colCount; j++) {
                seats.add(new Seat(i, j));
            }
        }
    }

    // 检查是否有售出票
    public boolean hasSoldTickets() {
        for (Seat seat : seats) {
            if (seat.isSold()) return true;
        }
        return false;
    }

    // 获取空闲座位数
    public int getAvailableSeatsCount() {
        int count = 0;
        for (Seat seat : seats) {
            if (!seat.isSold()) count++;
        }
        return count;
    }

    // 显示座位分布
    public void displaySeats() {
        if (seats == null || seats.isEmpty()) {
            System.out.println("无座位信息");
            return;
        }

        System.out.print("   "); // 行号占位
        // 获取最大列数（从座位列表推断）
        int maxCol = 0;
        for (Seat seat : seats) {
            if (seat.getCol() > maxCol) maxCol = seat.getCol();
        }
        for (int j = 1; j <= maxCol; j++) {
            System.out.printf("%2d", j);
        }
        System.out.println();

        int currentRow = 1;
        System.out.printf("%2d ", currentRow);
        for (Seat seat : seats) {
            if (seat.getRow() != currentRow) {
                currentRow = seat.getRow();
                System.out.println();
                System.out.printf("%2d ", currentRow);
            }
            System.out.printf("%2s", seat.getStatus());
        }
        System.out.println();
    }

    // 检查座位是否可用
    public boolean checkSeatAvailability(int row, int col) {
        Seat seat = findSeat(row, col);
        return seat != null && !seat.isSold();
    }

    // 占用座位
    public void occupySeats(List<Seat> selectedSeats) {
        for (Seat seat : selectedSeats) {
            Seat target = findSeat(seat.getRow(), seat.getCol());
            if (target != null) target.setSold(true);
        }
    }

    // 释放座位
    public void releaseSeat(Seat seat) {
        Seat target = findSeat(seat.getRow(), seat.getCol());
        if (target != null) target.setSold(false);
    }

    // 查找座位
    public Seat findSeat(int row, int col) {
        for (Seat seat : seats) {
            if (seat.getRow() == row && seat.getCol() == col) return seat;
        }
        return null;
    }

    // Getter和Setter
    public int getId() { return id; }
    public Movie getMovie() { return movie; }
    public void setMovie(Movie movie) { this.movie = movie; }
    public String getHall() { return hall; }
    public Date getTime() { return time; }
    public void setTime(Date time) { this.time = time; }
    public double getPrice() { return price; }
    public List<Seat> getSeats() { return seats; }
}